package org.example.sword2offer.primary;

/**
 * @author: lynn
 * @Descript:求根号3，保留后8位
 * 二分法: 小左变，大右边
 * @date: 2021/4/9 22:45
 * @version: 1.0
 */
public class Q64_2_Radical3 {
    public static void main(String[] args) {
        double res=getRadical(3.0);
        String resStr=String.valueOf(res);

        System.out.println(resStr.split("\\.")[0]
                +"."
                +(resStr.split("\\.")[1].length()>=8?resStr.split("\\.")[1].substring(0,8):resStr.split("\\.")[1]));
    }

    private static double getRadical(double n){
        double min=n/2;double max=n;
        double mid=(min+max)/2;
        while (Math.abs(mid*mid-n)>=0.00000001){
            if (mid*mid<n){
                min=(mid+min)/2;
            }else {
                max=(max+mid)/2;
            }
            mid=(min+max)/2;
        }
        return mid;
    }
}
